﻿
Partial Class WebForm_ManageEnlistment_QueryEnlistment
    Inherits My.MyUserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        ObjectDataSource1.SelectParameters("UOrganizationID").DefaultValue = Me.ddlOrganization.SelectedValue
        Me.GV.DataBind()
    End Sub

    Protected Sub GV_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GV.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If TypeOf DataBinder.Eval(e.Row.DataItem, "HaveAccess") Is DBNull Then
                TryCast(e.Row.FindControl("lbtnDelete"), LinkButton).Visible = False
                TryCast(e.Row.FindControl("mvForm"), MultiView).SetActiveView(TryCast(e.Row.FindControl("vwSelect"), View))
            ElseIf DataBinder.Eval(e.Row.DataItem, "HaveAccess") = "Y" Then
                TryCast(e.Row.FindControl("lbtnDelete"), LinkButton).Visible = True
                TryCast(e.Row.FindControl("mvForm"), MultiView).SetActiveView(TryCast(e.Row.FindControl("vwEdit"), View))
            Else
                TryCast(e.Row.FindControl("lbtnDelete"), LinkButton).Visible = False
                TryCast(e.Row.FindControl("mvForm"), MultiView).SetActiveView(TryCast(e.Row.FindControl("vwSelect"), View))
            End If
        End If
    End Sub

    '有相依性的資料不可以刪除
    Protected Sub GV_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GV.RowDeleting

        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        sql.AppendFormat("SELECT B.EnlistID ")
        sql.AppendFormat("FROM EnlistHeader A ")
        sql.AppendFormat("inner join EnlistRecordApplication B on A.EnlistID=B.EnlistID ")
        sql.AppendFormat("WHERE A.EnlistID='{0}' ", e.Keys.Item("EnlistID"))

        dt = dat.GetData(sql)

        If dt.Rows.Count > 0 Then
            e.Cancel = True
            MyObj.ShowMessage(Me.GetLocalResourceObject("CanNotDelete"))
        End If

    End Sub

#Region "Delegate"

    Public Event SelectedIndexChanged(ByVal source As Object, ByVal e As EventArgs)
    Public Event Editing(ByVal source As Object, ByVal e As EventArgs)

    Protected Sub GV_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GV.RowEditing
        Me.SelectedID = Me.GV.DataKeys(e.NewEditIndex)("EnlistID")
        RaiseEvent Editing(Me, New EventArgs)
        e.Cancel = True
    End Sub

    Protected Sub GV_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GV.SelectedIndexChanged
        Me.SelectedID = Me.GV.SelectedDataKey("EnlistID")
        RaiseEvent SelectedIndexChanged(Me, New EventArgs)
    End Sub

#End Region

    Protected Sub ObjectDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles ObjectDataSource1.Selecting
        e.InputParameters("Annual") = Me.AnnualControl1.Anaual
    End Sub

    Protected Sub ddlOrganization_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlOrganization.Load
        If Not IsPostBack Then
            Me.ddlOrganization.DataTextField = "OrganizationName"
            Me.ddlOrganization.DataValueField = "UOrganizationID"
            Me.ddlOrganization.DataSource = MyObj.GetDDLDirectOrganizationData()
            Me.ddlOrganization.DataBind()

            Me.ddlOrganization.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
        End If
    End Sub
End Class

Partial Class WebForm_ManageEnlistment_QueryEnlistment

    Public Sub InitData()

        'If Me.CanAdmin Then
        '    ObjectDataSource1.SelectParameters("UOrganizationID").DefaultValue = "Admin"
        'Else
        '    ObjectDataSource1.SelectParameters("UOrganizationID").DefaultValue = clsAuth.GetUser.UOrganizationID
        'End If

        GV.DataSourceID = ObjectDataSource1.ID
        GV.DataBind()

    End Sub

#Region "Property"

    Public Property SelectedID() As String
        Get
            'If Me.GV.SelectedRow Is Nothing Then
            '    Return 0
            'Else
            '    Return GV.SelectedValue
            'End If
            Return ViewState("SelectedID")
        End Get
        Set(ByVal value As String)
            ViewState("SelectedID") = value
        End Set
    End Property

#End Region

End Class